﻿using FinancialReportPowerPointAddIn.Helper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Drawing;
using System.Linq;

namespace FinancialReportPowerPointAddIn.PPTModule
{
    public static class GenerateHightLightSummary2
    {
        public static void Write(int year, int month, int comYear, int comMonth)
        {
            //获取当前ppt中所有的幻灯片
            var slides = Globals.ThisAddIn.Application.ActivePresentation.Slides;
            //在当前选中幻灯片前插入新的幻灯片--代码提示中没有F12进去才找到的方法
            Slide currentslide = slides.Add(slides.Count + 1, PpSlideLayout.ppLayoutBlank);
            currentslide.Select();
            //写入标题           
            Shape titleBox = currentslide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 0, 0, 370, 15);
            titleBox.TextFrame.TextRange.Text = $"各餐厅财务状况汇总 {year}{month.ToString("00")} YTD\r\n";
            titleBox.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoCTrue;
            //写入表格
            int rowsCount = (year - 1) == comYear ? 12 : 16;
            Shape tableShape = currentslide.Shapes.AddTable(rowsCount, 9, 0, 25, currentslide.Master.Width, currentslide.Master.Height - 25);
            Table table = tableShape.Table;
            //预设整理格式
            foreach (Row row in table.Rows)
            {
                foreach (Cell cell in row.Cells)
                {
                    cell.Shape.TextFrame.TextRange.Font.Name = "微软雅黑";
                    cell.Shape.TextFrame.TextRange.Font.Size = 15;
                    cell.Shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;
                    cell.Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;
                }
            }

            foreach (Cell cell in table.Rows[1].Cells)
            {
                cell.Shape.TextFrame.TextRange.Font.Size = 18;
            }

            // 编写标题行
            //写入MTD  要调转颜色RGB =>BGR
            int writeRow = 1;
            //1
            table.Cell(writeRow, 1).Shape.TextFrame.TextRange.Text = "YTD";
            //1
            table.Cell(writeRow, 1).Shape.Fill.ForeColor.RGB = Color.FromArgb(0, 0, 0).ToArgb();
            //1 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(255, 255, 255).ToArgb();
            //2 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = "YTD-PAC";
            //3 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = "YTD租金";
            //3-4 -- ++
            table.Cell((writeRow - 1), 1).Merge(table.Cell(writeRow++, 1));
            //5 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = "YTD-GA";
            //6 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = "YTD SOI";
            //或有
            if (rowsCount == 16)
            {
                //7++
                table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"{(year - 1).ToString().Substring(2)} YTD SOI";
                //8++
                table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"YTD SOI VS.{(year - 1).ToString().Substring(2)}";
            }
            //9++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"{comYear.ToString().Substring(2)} YTD SOI";
            //10 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"YTD SOI VS.{comYear.ToString().Substring(2)}";
            //11 ++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = "YTD债前现金流";
            //或有
            if (rowsCount == 11)
            {
                //12++
                table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"{(year - 1).ToString().Substring(2)} YTD债前现金流 ";
                //13++
                table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"YTD债前 VS.{(year - 1).ToString().Substring(2)}";
            }
            //14++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"{comYear.ToString().Substring(2)} YTD债前现金流";
            //15++
            table.Cell(writeRow++, 1).Shape.TextFrame.TextRange.Text = $"YTD债前 VS.{comYear.ToString().Substring(2)}";
            //16
            table.Cell(writeRow, 1).Shape.TextFrame.TextRange.Text = $"{year.ToString().Substring(2)}年YTD再投资";
            //开始写入数据            
            using (var db = new FinancialReportContext())
            {
                string[] restaurants = new string[] { "潭江店", "开大店", "白石店", "西粤店", "东湖店", "恩平店", "油十店", "汇总" };
                //先准备数据
                for (int i = 0; i < restaurants.Length; i++)
                {
                    writeRow = 1;
                    string flag = string.Empty;
                    string code = string.Empty;
                    Common.GetPLNameAndCode(restaurants[i], out flag, out code);
                    var ytdcpldata = db.PLDatas.Where(x => x.IsPlan == false && x.Year == year && x.Month <= month && x.Restaurant == flag).AsNoTracking().ToList();
                    var ytdlypldata = db.PLDatas.Where(x => x.IsPlan == false && x.Year == year - 1 && x.Month <= month && x.Restaurant == flag).AsNoTracking().ToList();
                    var ytdcompldata = db.PLDatas.Where(x => x.IsPlan == false && x.Year == comYear && x.Month <= comMonth && x.Restaurant == flag).AsNoTracking().ToList();
                    //开始写入   1++                
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = restaurants[i];
                    //PAC 2++
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = (ytdcpldata.Sum(x => x.可控利润) / ytdcpldata.Sum(x => x.产品净收入)).ToString("p");
                    //租金1  3++    
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdcpldata.Sum(x => x.租金).ToString("#,##0");
                    //租金2  4++       
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = (ytdcpldata.Sum(x => x.租金) / ytdcpldata.Sum(x => x.产品净收入)).ToString("p");
                    //GA     5++                         
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ((ytdcpldata.Sum(x => x.法律费用) + ytdcpldata.Sum(x => x.其他费用)) / ytdcpldata.Sum(x => x.产品净收入)).ToString("p");
                    //YTD SOI 6++
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdcpldata.Sum(x => x.营运利润).ToString("#,##0");
                    //或有
                    if (rowsCount == 16)
                    {
                        //YTD SOI .VS Last Year 7++
                        table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdlypldata.Sum(x => x.营运利润).ToString("#,##0");
                        //8++
                        WriteVar(table, writeRow++, i + 2, ytdcpldata.Sum(x => x.营运利润) - ytdlypldata.Sum(x => x.营运利润), "#,##0");
                    }
                    //YTD SOI .VS Com Year 9++
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdcompldata.Sum(x => x.营运利润).ToString("#,##0");
                    //10++
                    WriteVar(table, writeRow++, i + 2, ytdcpldata.Sum(x => x.营运利润) - ytdcompldata.Sum(x => x.营运利润), "#,##0");
                    //YTD 债前 11++
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdcpldata.Sum(x => x.债前现金流).ToString("#,##0");
                    //或有
                    if (rowsCount == 16)
                    {
                        //YTD 债前 .VS Last Year 12++或有
                        table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdlypldata.Sum(x => x.债前现金流).ToString("#,##0");
                        //13++
                        WriteVar(table, writeRow++, i + 2, ytdcpldata.Sum(x => x.债前现金流) - ytdlypldata.Sum(x => x.债前现金流), "#,##0");
                    }
                    //YTD 债前 .VS Com Year 14++
                    table.Cell(writeRow++, i + 2).Shape.TextFrame.TextRange.Text = ytdcompldata.Sum(x => x.债前现金流).ToString("#,##0");
                    //15++
                    WriteVar(table, writeRow++, i + 2, ytdcpldata.Sum(x => x.债前现金流) - ytdcompldata.Sum(x => x.债前现金流), "#,##0");
                    //16
                    table.Cell(writeRow, i + 2).Shape.TextFrame.TextRange.Text = ytdcpldata.Sum(x => x.再投资).ToString("#,##0");
                }
            }
            tableShape.Height = currentslide.Master.Height - 25;
            tableShape.Width = currentslide.Master.Width;
        }

        private static void WriteVar(Table table, int row, int col, decimal temp, string type)
        {
            if (temp >= 0)
            {
                table.Cell(row, col).Shape.TextFrame.TextRange.Text = "▲" + temp.ToString(type);
                table.Cell(row, col).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 205, 0).ToArgb();
            }
            else
            {
                table.Cell(row, col).Shape.TextFrame.TextRange.Text = "▼" + Math.Abs(temp).ToString(type);
                table.Cell(row, col).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 0, 255).ToArgb();
            }
        }
    }
}
